package com.jf3q.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.common.constant.SysConstant;
import com.jf3q.common.fegin.OssClient;
import com.jf3q.teacher.mapper.TeacherMapper;
import com.jf3q.teacher.pojo.Teacher;
import com.jf3q.teacher.service.TeacherService;
import com.jf3q.teacher.vo.SearchTeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 *
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher>
    implements TeacherService {

    @Autowired
    OssClient ossClient;

    @Override
    public Page<Teacher> getPage(Integer pageNum, SearchTeacherVo searchTeacherVo) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Teacher> page= new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNum, SysConstant.servicePageSize);
        QueryWrapper<Teacher> queryWrapper= new QueryWrapper();
        queryWrapper.orderByDesc("edu_modified");

        if (!ObjectUtils.isEmpty(searchTeacherVo.getName())) {
            queryWrapper.like("name",searchTeacherVo.getName());
        }
        if (!ObjectUtils.isEmpty(searchTeacherVo.getLevel())) {
            queryWrapper.eq("level",searchTeacherVo.getLevel());
        }
        if (!ObjectUtils.isEmpty(searchTeacherVo.getListDate())) {
            queryWrapper.between("edu_create",searchTeacherVo.getListDate().get(0),searchTeacherVo.getListDate().get(1));
        }

        Page<Teacher> iPage = baseMapper.selectPage(page, queryWrapper);
        return iPage;
    }

    @Override
    public Boolean del(String id) {
        try {
            LambdaUpdateWrapper<Teacher> updateWrapper = new UpdateWrapper<Teacher>().lambda();
            updateWrapper.set(Teacher::getIsDeleted, 1).eq(Teacher::getId, id);
            baseMapper.update(new Teacher(), updateWrapper);

            //删除了老师的基本信息后，，应该把老师的头像文件也删掉

            Teacher teacher = baseMapper.selectById(id);

            //远程调用  oss-service里面的删除接口
            ossClient.del(teacher.getAvatar());
            return true;
        } catch (Exception e) {
            return false;
        }

    }


}




